package com.aaa.pdfdemo;

import android.content.Context;
import android.util.Log;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.listener.OnPageErrorListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.github.barteksc.pdfviewer.util.FitPolicy;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * create by water.yuan
 * on 2020-09-06
 */
public class OpenAccountPdfManager {
    private static final String TAG = "OpenAccountPdfManager";

    private static final int BORDER_LEFT = 4;
    private static final int BORDER_RIGHT = 8;
    private static final int TITLE_PADDING_WIDTH = 19;
    private static final float TABLE_WIDTH = 94.0f;
    private static final BaseColor BORDER_COLOR = new BaseColor(234, 244, 250);
    private static final BaseColor TITLE_COLOR = new BaseColor(66, 104, 219);
    private static final BaseColor CELL_NAME_COLOR = new BaseColor(142, 142, 147);
    private static final BaseColor TITLE_FONT2_COLOR = new BaseColor(142, 164, 233);
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy年MM月dd日");
    private static SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

    private static volatile OpenAccountPdfManager instance;

    private Context context;

    private OpenAccountPdfManager(Context context) {
        this.context = context;
    }

    public static OpenAccountPdfManager getInstance(Context context) {
        if (instance == null) {
            synchronized (OpenAccountPdfManager.class) {
                if (instance == null) {
                    instance = new OpenAccountPdfManager(context);
                }
            }
        }
        return instance;
    }

    public void readPdf(PDFView pdfView) {
        OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
            @Override
            public void onPageChanged(int i, int i1) {

            }
        };

        OnLoadCompleteListener onLoadCompleteListener = new OnLoadCompleteListener() {
            @Override
            public void loadComplete(int i) {

            }
        };

        OnPageErrorListener onPageErrorListener = new OnPageErrorListener() {

            @Override
            public void onPageError(int i, Throwable throwable) {

            }
        };

        String absolutePath = context.getExternalCacheDir().getAbsolutePath();
        File file = new File(absolutePath, "test3.pdf");

        pdfView.fromFile(file)
                .defaultPage(0)
                .onPageChange(onPageChangeListener)
                .enableAnnotationRendering(true)
                .onLoad(onLoadCompleteListener)
                .scrollHandle(new DefaultScrollHandle(context))
                .spacing(10) // in dp
                .onPageError(onPageErrorListener)
                .pageFitPolicy(FitPolicy.BOTH)
                .load();
    }

    public void createPdf() {
        String absolutePath = context.getExternalCacheDir().getAbsolutePath();
        File file = new File(absolutePath, "test3.pdf");
        Log.i(TAG, "createPdf: file=" + file);
        if (file != null && file.exists()) {
            boolean delete = file.delete();
            Log.i(TAG, "createPdf: delete=" + delete);
        }

        Rectangle pageSize = new Rectangle(0.0F, 0.0F, 595.0F, 842.0F);
        pageSize.setBackgroundColor(BORDER_COLOR);

        FileOutputStream fos = null;
        Document document = null;
        PdfWriter pWriter = null;
        BaseFont font = null;
        try {
            fos = new FileOutputStream(file);
            document = new Document(pageSize, 10, 10, 20, 20);
            pWriter = PdfWriter.getInstance(document, fos);

            font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font1 = new Font(font, 10, Font.BOLD, TITLE_COLOR);
            Font font2 = new Font(font, 9, Font.NORMAL);
            Font ft = new Font(font, 9, Font.NORMAL);
            Font cellNameFt = new Font(font, 9, Font.NORMAL, BaseColor.BLACK);
            Font titleFont2 = new Font(font, 8, Font.NORMAL, TITLE_FONT2_COLOR);

            document.open();

            // 添加空行
            document.add(Chunk.NEWLINE);

            Paragraph title = new Paragraph("开立单位银行结算账户申请书", new Font(font, 12, Font.BOLD, TITLE_COLOR));
            title.setSpacingBefore(10);
            title.setSpacingAfter(10);
            title.setAlignment(Paragraph.ALIGN_CENTER);// 居中
            document.add(title);

            // 添加空行
            document.add(Chunk.NEWLINE);

            // 存款人
            PdfPTable table = new PdfPTable(4);
            table.setWidthPercentage(TABLE_WIDTH);
            table.addCell(ItextBuilder.getPdfNameCell("存款人名称", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("成都XX公司", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("电话", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("183xxxx2513", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("地址", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("中国（四川）自由贸易区", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("邮编", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("610000", ft, 3));
            document.add(table);

            // 财务联系人
            table = new PdfPTable(4);
            table.setSpacingBefore(20f);
            table.setWidthPercentage(TABLE_WIDTH);
            table.addCell(ItextBuilder.getPdfNameCell("财务联系人", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("xx", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("传真号码", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("xx", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("存款人类别", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("企业法人", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("组织机构代码", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("xx", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("■ 法定代表人", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell("□ 单位负责人", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell(" ", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell(" ", cellNameFt, 1));

            table.addCell(ItextBuilder.getPdfNameCell("姓名", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("证件种类", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("0-居民身份证", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("证件号码", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("行业分类", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("注册资金", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("(元)", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("货币种类", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("01-人民币", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("地区代码", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("610000", ft, 3));

            table.addCell(ItextBuilder.getPdfNameCell("经营范围", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("经营范围经营范围经营范围经营范围经营范围经营范围" +
                    "经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围" +
                    "经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围经营范围", ft, 3, 6f));
            table.addCell(ItextBuilder.getPdfNameCell("证明文件种类", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("01-工商营业执照", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("证明文件编号", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 1));

            table.addCell(ItextBuilder.getPdfNameCell("国税登记证", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("xx", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("地税登记证", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("xx", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("注册类型", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("15-有限责任公司", ft, 3));
            document.add(table);

            table = new PdfPTable(4);
            table.setSpacingBefore(20f);
            table.setWidthPercentage(TABLE_WIDTH);
            table.addCell(ItextBuilder.getPdfNameCell("==关联企业信息填列在\"关联企业登记表\"上==", ft, 4));
            table.addCell(ItextBuilder.getPdfNameCell("账户性质", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("1-基本账户", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("资金性质", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("有效期至", cellNameFt, 2));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 2));
            document.add(table);

            table = new PdfPTable(4);
            table.setSpacingBefore(20f);
            table.setWidthPercentage(TABLE_WIDTH);
            table.addCell(ItextBuilder.getPdfNameCell("==以下为存款人上级法人或主管单位信息==", ft, 4));
            table.addCell(ItextBuilder.getPdfNameCell("上级法人或者主管单位名称", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("xx", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("基本存款账户编号/开户许可证核准编号", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("组织机构代码", cellNameFt, 2));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 2));
            table.addCell(ItextBuilder.getPdfNameCell("■ 法定代表人", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell("□ 单位负责人", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell(" ", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell(" ", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfNameCell("姓名", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("证件种类", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("0-居民身份证", ft, 1));
            table.addCell(ItextBuilder.getPdfNameCell("证件号码", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 1));
            document.add(table);

            table = new PdfPTable(4);
            table.setSpacingBefore(20f);
            table.setWidthPercentage(TABLE_WIDTH);
            table.addCell(ItextBuilder.getPdfNameCell("账户名称", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("开户银行名称", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("基本存款账户编号/开户许可证核准编号", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            table.addCell(ItextBuilder.getPdfNameCell("开户银行代码", cellNameFt, 1));
            table.addCell(ItextBuilder.getPdfValueCell("", ft, 3));
            document.add(table);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (document != null) {
            document.close();
            document = null;
        }

        if (pWriter != null) {
            pWriter.flush();
            pWriter.close();
            pWriter = null;
        }

        if (fos != null) {
            try {
                fos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (fos != null) {
            try {
                fos.close();
                fos = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        new Thread(() -> {
            try {
                Thread.sleep(2000);
                Log.i(TAG, "createPdf: exit");
//                System.exit(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

}
